import request from '../../utils/request';

export interface OrderItemVO {
  id?: number;
  orderId?: number;
  spuId: number;
  spuName: string;
  skuId: number;
  picUrl: string;
  count: number;
  price: number;
  discountPrice?: number;
  deliveryTemplateId?: number;
  properties?: any[];
}

export interface OrderVO {
  id?: number;
  no?: string;
  type: number;
  terminal: number;
  userId: number;
  userIp: string;
  userRemark?: string;
  status: number;
  productCount: number;
  totalPrice: number;
  discountPrice: number;
  deliveryPrice: number;
  adjustPrice: number;
  payPrice: number;
  payTime?: string;
  deliveryTime?: string;
  receiveTime?: string;
  commentTime?: string;
  cancelTime?: string;
  cancelType?: number;
  remark?: string;
  payOrderId?: number;
  payChannelCode?: string;
  deliveryType: number;
  deliveryTemplateId?: number;
  logisticsId?: number;
  logisticsNo?: string;
  receiverName: string;
  receiverMobile: string;
  receiverAreaId: number;
  receiverDetailAddress: string;
  afterSaleStatus: number;
  refundPrice: number;
  couponId?: number;
  couponPrice: number;
  pointPrice: number;
  vipPrice: number;
  createTime?: string;
  items?: OrderItemVO[];
}

export interface OrderDeliveryVO {
  id: number;
  logisticsId: number;
  logisticsNo: string;
}

// 订单管理 API
export const OrderApi = {
  // 查询订单分页
  getOrderPage: async (params: any) => {
    return await request.get('/trade/order/page', { params });
  },

  // 查询订单详情
  getOrder: async (id: number) => {
    return await request.get(`/trade/order/get?id=${id}`);
  },

  // 订单发货
  deliveryOrder: async (data: OrderDeliveryVO) => {
    return await request.put('/trade/order/delivery', data);
  },

  // 订单备注
  updateOrderRemark: async (id: number, remark: string) => {
    return await request.put('/trade/order/update-remark', { id, remark });
  },

  // 调整订单价格
  updateOrderPrice: async (id: number, price: number) => {
    return await request.put('/trade/order/update-price', { id, price });
  },

  // 关闭订单
  closeOrder: async (id: number) => {
    return await request.put(`/trade/order/close?id=${id}`);
  },

  // 导出订单
  exportOrder: async (params: any) => {
    return await request.get('/trade/order/export-excel', { params, responseType: 'blob' });
  },

  // 获取订单统计
  getOrderStatistics: async (params?: any) => {
    return await request.get('/trade/order/statistics', { params });
  }
};